package com.huawei.petSystemdemo.ui.notifications;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huawei.hms.hwid.I;
import com.huawei.petSystemdemo.BaseFragment;
import com.huawei.petSystemdemo.GlobalViewModel;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.adapter.ShoppingAdapter;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.bean.Shopping;
import com.huawei.petSystemdemo.data.CartRepository;
import com.huawei.petSystemdemo.data.ShoppingRepository;
import com.huawei.petSystemdemo.data.UserRepository;
import com.huawei.petSystemdemo.databinding.FragmentNotificationsBinding;
import com.huawei.petSystemdemo.ui.mOrder.OrderSureActivity;
import com.huawei.petSystemdemo.utils.MyRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class NotificationsFragment extends BaseFragment {

    private NotificationsViewModel notificationsViewModel;
    private FragmentNotificationsBinding binding;
    private Map<String, Object> resBody = new HashMap<>();
    private TextView shopping_buy;
    public Map<Integer, Integer> chat = new HashMap<>();
    private List<Shopping> shoppingList = new ArrayList<>();
    public List<Shopping> shoppingSelected = new ArrayList<>();
    private TextView shopping_remove;
    private GlobalViewModel global = GlobalViewModel.getGlobal();

    TextView totalPrice;
    TextView shopping_num;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel = new ViewModelProvider(this).get(NotificationsViewModel.class);

        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        shopping_buy = root.findViewById(R.id.shopping_buy);
        totalPrice = root.findViewById(R.id.shopping_total);
        shopping_num = root.findViewById(R.id.shopping_num);
        RecyclerView list = root.findViewById(R.id.shopping_recycle);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(RecyclerView.VERTICAL);
        list.setLayoutManager(manager);

        List<Pets> selected = new ArrayList<>();
        getGlobal().seleted.setValue(selected);

//        if (GlobalViewModel.getGlobal().isLogin())
//            GlobalViewModel.getGlobal().user.getId();

        //判断用户是否登录
        if(getGlobal().user == null) {
            Toast.makeText(getContext(), "未登录", Toast.LENGTH_SHORT).show();
        } else {
            int userId = GlobalViewModel.getGlobal().user.getId();

            //获得用户购物车数据
            ShoppingRepository.getShoppingByUserId(userId, new Consumer<List<Shopping>>() {
                @Override
                public void accept(List<Shopping> shoppings) {
                    shoppingList.addAll(shoppings);
                    ShoppingAdapter adapter = new ShoppingAdapter(shoppingList, getContext());
                    list.setAdapter(adapter);
                }
            });
        }

        //数据实时监控
        getGlobal().seleted.observe(getViewLifecycleOwner(), new Observer<List<Pets>>() {
            @Override
            public void onChanged(List<Pets> pets) {
                totalPrice.setText(getGlobal().getTotalPrice() + "");
                shopping_num.setText("(" + pets.size() + ")");
            }
        });

        //结算
        shopping_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), OrderSureActivity.class);
                startActivity(intent);
            }
        });


        //删除
        shopping_remove = root.findViewById(R.id.shopping_remove);
        shopping_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(getGlobal().shoppingSelected.getValue().size() == 0) {
                    Toast.makeText(getContext(), "还没有选择商品", Toast.LENGTH_SHORT).show();
                }
                else {
                    //获取购物车中的项目Id
                    List<Integer> listShoppingId = new ArrayList<>();
                    for (int i = 0; i < getGlobal().shoppingSelected.getValue().size(); i++) {
                        listShoppingId.add(getGlobal().shoppingSelected.getValue().get(i).getId());
                    }

                    CartRepository.deleteShopping(listShoppingId, new Consumer<String>() {
                        @Override
                        public void accept(String data) {
                            Toast.makeText(getContext(), data, Toast.LENGTH_SHORT).show();
                            getGlobal().getTotalPrice();

                            //刷新购物车
                            ShoppingRepository.getShoppingByUserId(getGlobal().user.getId(), new Consumer<List<Shopping>>() {
                                @Override
                                public void accept(List<Shopping> shoppings) {
                                    shoppingList.clear();
                                    shoppingList.addAll(shoppings);
                                    ShoppingAdapter adapter = new ShoppingAdapter(shoppingList, getContext());
                                    list.setAdapter(adapter);
                                    //刷新global中的值
                                    List<Shopping> global_shooping_clear = new ArrayList<>();
                                    getGlobal().shoppingSelected.setValue(global_shooping_clear);
                                    List<Pets> global_pet_clear = new ArrayList<>();
                                    getGlobal().seleted.setValue(global_pet_clear);
                                }
                            });
                        }
                    });
                }
            }
        });
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


}